﻿using System.Globalization;
using Fuse8.ContentManagementFramework.Administration.Attributes;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.SystemFacade.Globalization;
using Fuse8.DomainFramework.BusinessService;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Fuse8.ContentManagementFramework.Administration.Models
{
    public class CmsContentModel : CmsSiteAwareModel, IValidatableObject
    {
        public CmsContentModel(int id, int typeId)
        {
            ContentTypeId = typeId;
            var item = new BusinessServiceFacade<CmsContent, int>().FindOne(id);
            if (item != null)
            {
                ContentBody = item.ContentBody;
                ContentType = item.ContentType;
                ContentTitle = item.ContentTitle;
                ContentSummary = item.ContentSummary;
                MetaInformation = item.EntityMetaInformation;
                ContentTypeId = item.ContentTypeId;
                PublishStartDate = item.PublishStartDate;
                PublishEndDate = item.PublishEndDate;
                Id = id;
                PublicContentId = item.PublicContentId;
            }
            else if (typeId != 0)
            {
                ContentType = new CmsContentTypeService().FindOne(typeId);
                MetaInformation = new CmsContent() { ContentTypeId = typeId }.EntityMetaInformation;
            }
            EditMode = EditMode.Usual;
        }

        public CmsContentModel()
        {
            EditMode = EditMode.Usual;
        }

        public CmsEntityMetaInformation MetaInformation { get; protected set; }

        public IList<CmsEntityMetaModel> Metas { get; set; }

        [GlobalizedRequired("Please enter title", "cms_Content")]
        public string ContentTitle { get; set; }

        public string ContentSummary { get; set; }

        public CmsContentType ContentType { get; set; }

        public string ContentBody { get; set; }

        public int ContentTypeId { get; set; }

        public DateTime? PublishEndDate { get; set; }

        public DateTime? PublishStartDate { get; set; }

        public int PublicContentId { get; set; }

        public EditMode EditMode { get; set; }

        public CmsContent ToDomainEntity()
        {
            var item = new BusinessServiceFacade<CmsContent, int>().FindOne(Id) ?? new CmsContent();

            item.ContentBody = ContentBody;
            item.ContentTitle = ContentTitle;
            item.ContentSummary = ContentSummary;
            item.ContentTypeId = ContentTypeId;
            item.PublishEndDate = PublishEndDate;
            item.PublishStartDate = PublishStartDate;
            item.IsPublished = true;

            return item;
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            int i = 0;
            if (Metas != null)
            {
                var errorMessage =
                    GlobalizationFacade.Current.WithDomain("cms_Content", CultureInfo.CurrentUICulture)
                        .GetText("Field is required");
                foreach (var meta in Metas)
                {
                    if (meta.IsRequired && meta.Value == null)
                    {
                        yield return
                            new ValidationResult(errorMessage, new[] { string.Format("Metas[{0}].Value", i) });
                    }
                    i++;
                }
            }
        }
    }

    public enum EditMode
    {
        Usual = 1,
        Revision = 2,
        Deleted = 3
    }
}